<?php
namespace app\admin\controller;
class Area extends Base {
    public function __construct() {
        parent::__construct();
    }
    
    public function text($str){
        $i = strpos($str, '(');
        if($i !== false){
            $str = substr($str, 0, $i + 1);
        }
        return $str;
    }
    
    public function create() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        if($this->role != 1){
            return errorResult();
        }
        
        $result = file_get_contents('https://apis.map.qq.com/ws/district/v1/list?key=4RFBZ-DVIKW-KIARB-OAM52-IKSZK-KBBRE');
        $result = json_decode($result, true);
        $list = $result['result'];
        
        $province = $list[0];
        $city = $list[1];
        $district = $list[2];
        
        $sqls = [];
        $id = 1030;
        foreach($province as $p){
            $cids = $p['cidx'];
            $pid = $id;
            $id ++;
            
            $p['fullname'] = $this->text($p['fullname']);
            $data = ['id' => $pid, 'level' => 1, 'name' => $p['fullname'], 'longitude' => $p['location']['lng'], 'latitude' => $p['location']['lat'], 'code' => $p['id']];
            if(isset($p['pinyin'])){
                $data['pinyin'] = $p['pinyin'][0] . $p['pinyin'][1];
            }
            /* var_dump($data);
             return 222; */
            array_push($sqls, $data);
            if(substr_count($p['fullname'], '市') || substr_count($p['fullname'], '特别行政区')){
                $cid = $id;
                $id ++;
                
                $data = ['id' => $cid, 'level' => 2, 'name' => $p['fullname'], 'longitude' => $p['location']['lng'], 'latitude' => $p['location']['lat'], 'parent_id' => $pid, 'code' => $p['id']];
                if(isset($p['pinyin'])){
                    $data['pinyin'] = $p['pinyin'][0] . $p['pinyin'][1];
                }
                array_push($sqls, $data);
                
                for($i=$cids[0]; $i<=$cids[1]; $i ++){
                    $cid = $id;
                    $id ++;
                    $c = $city[$i];
                    $c['fullname'] = $this->text($c['fullname']);
                    $data = ['id' => $cid, 'level' => 3, 'name' => $c['fullname'], 'longitude' => $c['location']['lng'], 'latitude' => $c['location']['lat'], 'parent_id' => $pid, 'code' => $c['id']];
                    if(isset($c['pinyin'])){
                        $data['pinyin'] = $c['pinyin'][0] . $c['pinyin'][1];
                    }
                    array_push($sqls, $data);
                    
                }
                
                
            }else{
                for($i=$cids[0]; $i<=$cids[1]; $i ++){
                    $cid = $id;
                    $id ++;
                    $c = $city[$i];
                    $c['fullname'] = $this->text($c['fullname']);
                    $data = ['id' => $cid, 'level' => 2, 'name' => $c['fullname'], 'longitude' => $c['location']['lng'], 'latitude' => $c['location']['lat'], 'parent_id' => $pid, 'code' => $c['id']];
                    if(isset($c['pinyin'])){
                        $data['pinyin'] = $c['pinyin'][0] . $c['pinyin'][1];
                    }
                    array_push($sqls, $data);
                    
                    if(isset($c['cidx'])){
                        $dids = $c['cidx'];
                        
                        for($j=$dids[0]; $j<=$dids[1]; $j ++){
                            $did = $id;
                            $id ++;
                            $d = $district[$j];
                            $d['fullname'] = $this->text($d['fullname']);
                            $data = ['id' => $did, 'level' => 3, 'name' => $d['fullname'], 'longitude' => $d['location']['lng'], 'latitude' => $d['location']['lat'], 'parent_id' => $cid, 'code' => $d['id']];
                            if(isset($d['pinyin'])){
                                $data['pinyin'] = $d['pinyin'][0] . $d['pinyin'][1];
                            }
                            array_push($sqls, $data);
                        }
                        
                    }
                }
            }
        }
        $model = model('Area');
        foreach ($sqls as $k => $item){
            $rs = $model->add($item);
            /* if($k == 0){
             var_dump($item);
             var_dump($model->getLastSql());
             break;
             } */
            if(!$rs){
                return 'fail';
            }
        }
        
        return 'success';
    }
    
    public function getArea() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        
        $id = request()->get('id', 0, 'intval');
        $area = model('Area')->findAll(['parent_id' => $id], ['name', 'id']);
        return successResult(['list' => $area]);
    }
    
}